package com.wangyk.rabbitmqconsole.configRabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: Topic Exchange是RabbitMQ中最灵活的一种方式，
 * 它能够根据routing_key自由的绑定不同的队列，可以适用绝大部分的项目需求
 * @Author: wangyk
 * @CreateDate: 2018/11/21$ 9:20$
 * @UpdateUser: wangyk
 * @UpdateDate: 2018/11/21$ 9:20$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Configuration
public class RabbitMqTopicConfig
{
    /**
     * 只接一个topic
     */
    final static String message = "topic.message";

    /**
     * 接收多个topic
     */
    final static String messages = "topic.messages";

    @Bean
    public Queue queueMessage()
    {
        return new Queue(RabbitMqTopicConfig.message);
    }

    @Bean
    public Queue queueMessages()
    {
        return new Queue(RabbitMqTopicConfig.messages);
    }

    @Bean
    TopicExchange exchange()
    {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange)
    {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange)
    {
        //这里的#表示零个或多个词。
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}
